package com.example.homeworkmutualevaluation.Controller;

import com.example.homeworkmutualevaluation.Entity.Administrator;
import com.example.homeworkmutualevaluation.Service.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdministratorController {
    @Resource
    private AdministratorService administratorService;
    @Resource
    private StudentService studentService;
    @Resource
    private TeacherService teacherService;
    @Resource
    private CourseService courseService;
    @Resource
    private TeachingService teachingService;
    @Resource
    private SCService scService;
    @Resource
    private BulletinService bulletinService;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestParam String id, @RequestParam String password){
        HashMap<String, Object> response = new HashMap<>();
        Administrator administrator = administratorService.find(id, password);
        if (administrator != null) {
            response.put("code", 1);
            response.put("msg", "登入成功");
            response.put("admin", administrator);
        } else {
            response.put("code", 0);
            response.put("msg", "账号或密码错误");
        }
        return response;
    }

    @PostMapping("/all_student")
    public Map<String, Object> allStudent() {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", studentService.findAll());
        return response;
    }

    @PostMapping("/all_teacher")
    public Map<String, Object> allTeacher() {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", teacherService.findAll());
        return response;
    }

    @PostMapping("/change_student_password")
    public Map<String, Object> changeStudentPassword(@RequestParam String phone, @RequestParam String password) {
        HashMap<String, Object> response = new HashMap<>();
        if (studentService.changePasswordByAdmin(phone, password)) {
            response.put("code", 1);
            response.put("msg", "更改成功");
        } else {
            response.put("code", 0);
            response.put("msg", "未知错误");
        }
        return response;
    }

    @PostMapping("/change_teacher_password")
    public Map<String, Object> changeTeacherPassword(@RequestParam String phone, @RequestParam String password) {
        HashMap<String, Object> response = new HashMap<>();
        if (teacherService.changePasswordByAdmin(phone, password)) {
            response.put("code", 1);
            response.put("msg", "更改成功");
        } else {
            response.put("code", 0);
            response.put("msg", "未知错误");
        }
        return response;
    }

    @PostMapping("/create_course")
    public Map<String, Object> createCourse(@RequestParam String cno, @RequestParam String cname) {
        HashMap<String, Object> response = new HashMap<>();
        if (courseService.createCourse(cno, cname)) {
            response.put("code", 1);
            response.put("msg", "创建成功");
        } else {
            response.put("code", 0);
            response.put("msg", "课程号已存在");
        }
        return response;
    }

    @PostMapping("/del_course")
    public Map<String, Object> delCourse(@RequestParam String cno) {
        HashMap<String, Object> response = new HashMap<>();
        if (courseService.delete(cno)) {
            response.put("code", 1);
            response.put("msg", "删除成功");
        } else {
            response.put("code", 0);
            response.put("msg", "课程号不存在");
        }
        return response;
    }

    @PostMapping("/all_sc")
    public Map<String, Object> allSC() {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", scService.findAll());
        return response;
    }

    @PostMapping("/allocate_course")
    public Map<String, Object> allocateCourse(@RequestParam String tphone, @RequestParam String cno) {
        HashMap<String, Object> response = new HashMap<>();
        if (!teacherService.exist(tphone)) {
            response.put("code", 0);
            response.put("msg", "教师不存在");
        } else if (!courseService.exist(cno)) {
            response.put("code", 0);
            response.put("msg", "课程不存在");
        } else if (teachingService.allocate(tphone, cno)) {
            response.put("code", 1);
            response.put("msg", "课程分配成功");
        } else {
            response.put("code", 0);
            response.put("msg", "课程已经分配给该老师");
        }
        return response;
    }

    @PostMapping("/all_teaching")
    public Map<String, Object> allTeaching() {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", teachingService.findAll());
        return response;
    }

    @PostMapping("/deallocate_course")
    public Map<String, Object> deallocateCourse(@RequestParam String tphone, @RequestParam String cno){
        HashMap<String, Object> response = new HashMap<>();
        if (!teacherService.exist(tphone)) {
            response.put("code", 0);
            response.put("msg", "教师不存在");
        } else if (!courseService.exist(cno)) {
            response.put("code", 0);
            response.put("msg", "课程不存在");
        } else if (teachingService.delete(tphone, cno)) {
            response.put("code", 1);
            response.put("msg", "课程取消分配成功");
        } else {
            response.put("code", 0);
            response.put("msg", "课程尚未分配给该老师");
        }
        return response;
    }

    @PostMapping("/ban_student")
    public Map<String, Object> banStudent(@RequestParam String sphone, @RequestParam String bannedtime) {
        HashMap<String, Object> response = new HashMap<>();
        Timestamp timestamp;
        try {
            timestamp = Timestamp.valueOf(bannedtime);
        } catch (java.lang.IllegalArgumentException e) {
            response.put("code", 0);
            response.put("msg", "时间格式错误或时间不合法(必须为yyyy-mm-dd hh:mm:ss)");
            return response;
        }
        if (studentService.updateBannedtime(sphone, timestamp)) {
            response.put("code", 1);
            response.put("msg", "成功封禁学生至[" + timestamp + "]");
        } else {
            response.put("code", 0);
            response.put("msg", "学生不存在");
        }
        return response;
    }

    @PostMapping("/del_student")
    public Map<String, Object> delStudent(@RequestParam String sphone) {
        HashMap<String, Object> response = new HashMap<>();
        if (studentService.delete(sphone)) {
            response.put("code", 1);
            response.put("msg", "删除成功");
        } else {
            response.put("code", 0);
            response.put("msg", "学生不存在");
        }
        return response;
    }

    @PostMapping("/del_teacher")
    public Map<String, Object> delTeacher(@RequestParam String tphone) {
        HashMap<String, Object> response = new HashMap<>();
        if (teacherService.delete(tphone)) {
            response.put("code", 1);
            response.put("msg", "删除成功");
        } else {
            response.put("code", 0);
            response.put("msg", "教师不存在");
        }
        return response;
    }

    @PostMapping("/new_bulletin")
    public Map<String, Object> newBulletin(@RequestParam String adminid, @RequestParam String title, @RequestParam String content) {
        HashMap<String, Object> response = new HashMap<>();
        int bulletinid = bulletinService.insert(adminid, title, content);
        response.put("code", 1);
        response.put("bulletinid", bulletinid);
        return response;
    }

    @PostMapping("/update_bulletin")
    public Map<String, Object> updateBulletin(@RequestParam int bulletinid, @RequestParam String title, @RequestParam String content) {
        HashMap<String, Object> response = new HashMap<>();
        if (bulletinService.update(bulletinid, title, content)) {
            response.put("code", 1);
            response.put("msg", "更新成功");
        } else {
            response.put("code", 0);
            response.put("msg", "公告不存在");
        }
        return response;
    }

    @PostMapping("/del_bulletin")
    public Map<String, Object> delBulletin(@RequestParam int bulletinid) {
        HashMap<String, Object> response = new HashMap<>();
        if (bulletinService.delete(bulletinid)) {
            response.put("code", 1);
            response.put("msg", "删除成功");
        } else {
            response.put("code", 0);
            response.put("msg", "公告不存在");
        }
        return response;
    }
}
